package com.example.hello;


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.example.hello.CategoryTable;
import com.example.hello.FullscreenActivity;

public class TestPackReader extends DefaultHandler {
		
	Map<Integer, String> _option_list = new HashMap<Integer, String>();
	private int _test_flag = 0;
	private int _test_suite_flag = 0;
	private int _q_flag = 0;
	private int _char_read_flag = 0;
	private int _q_desc_flag = 0;
	private int _op_flag = 0;
	private String _xml_file_name;
	int _q_count = 0;
	int _sub_cat_id;
	
	public TestPack _tp_instance = new TestPack();
	
	public TestPackReader(String _xml_file_name){
		this._xml_file_name = _xml_file_name;
	}
	
	public TestPackReader() {
		
	}

	public void initialize(){
		try{			
			//System.exit(1);
			
			//Create a "parser factory" for creating SAX parsers
	        SAXParserFactory spfac = SAXParserFactory.newInstance();
	        
	        //Now use the parser factory to create a SAXParser object
	        SAXParser sp = spfac.newSAXParser();
	        
            //Create an instance of this class; it defines all the handler methods
	        TestPackReader handler = new TestPackReader();

            //Finally, tell the parser to parse the input and notify the handler
            sp.parse(_xml_file_name, handler);
                       
		}
		catch(Exception e){
			System.err.println(e.getStackTrace().toString());
			System.err.println(e.getMessage().toString());
			
		}
	}
	
	
	public void characters(char[] buffer, int start, int length){
		if(_char_read_flag == 1){
			if(_q_desc_flag == 1){
				_tp_instance.set_q_desc(new String(buffer, start, length));				
			}
			if(_op_flag == 1){
				_option_list.put(_tp_instance.get_option_id(), new String(buffer, start, length));
			}
		}
	}
	
	
	public void startElement(String uri, String localName,
			String qName, Attributes attributes) throws SAXException {		
		
		if(qName.equalsIgnoreCase("TestSuite")){
			_test_suite_flag = 1;			
		}
		//this step will run if SML have TestSuite tag
		if(_test_suite_flag == 1){			
			if(qName.equalsIgnoreCase("Test")){
				_test_flag = 1;
				_tp_instance.set_test_name(attributes.getValue("name"));
				_tp_instance.set_cat_name(attributes.getValue("cat"));
				_tp_instance.set_sub_cat_name(attributes.getValue("subcat"));
				
				// insert details into category		
				String _table_field_names = "cat_name";
				String _table_field_values = _tp_instance.get_cat_name();
				
				
				//new QueryMaker().createInsertQuery("category", _table_field_names, _table_field_values);
				
				_table_field_names = "cat_id";
				//_temp_id store the ID of recent category.
				//Map<String,String> _select_result = new QueryMaker().selectQuery("category", _table_field_names, true);
				
				//int _cat_id = Integer.parseInt(_select_result.get("cat_id")); // uncomment when connect with DB after implementation of QuerMaker class
				int _cat_id = 1;
				// insert details into sub category
				_table_field_names = "cat_id, sub_cat_name, test_name";
				_table_field_values = _cat_id+", "+_tp_instance.get_sub_cat_name()+", "+_tp_instance.get_test_name();				
				//new QueryMaker().createInsertQuery("sub_category", _table_field_names, _table_field_values);
				
				//_select_result.clear(); // uncomment when connect with DB after implementation of QuerMaker class
				_table_field_names = "sub_cat_id";
				//_select_result = new QueryMaker().selectQuery("sub_category", _table_field_names, true);
				//_sub_cat_id = Integer.parseInt(_select_result.get("sub_cat_id")); // uncomment when connect with DB after implementation of QuerMaker class
				_sub_cat_id = 1;
			}						
			if(_test_flag == 1){
				if(qName.equalsIgnoreCase("q")){				
					_q_flag = 1;
					_tp_instance.set_level(Integer.parseInt(attributes.getValue("level").toString()));
					_tp_instance.set_ans(Integer.parseInt(attributes.getValue("ans").toString()));					
				}
				if(_q_flag == 1){
					if(qName.equalsIgnoreCase("q_desc")){					
						_char_read_flag = 1;
						_q_desc_flag = 1;
					}
					if(qName.equalsIgnoreCase("op")){					
						_char_read_flag = 1;
						_op_flag = 1;
						_tp_instance.set_option_id(Integer.parseInt(attributes.getValue("id").toString()));
					}
				}
			}
			
		}	
			
	}
	
	
	public void endElement(String uri, String localName, 
			String qName)throws SAXException {
		
		if(qName.equalsIgnoreCase("TestSuite")){
			_test_suite_flag = 0;
		}
		if(qName.equalsIgnoreCase("Test")){
			_test_flag = 0;
		}		
		if(qName.equalsIgnoreCase("q_desc")){
			_q_desc_flag = 0;
		}
		if(qName.equalsIgnoreCase("op")){
			_op_flag = 0;
		}
		if(qName.equalsIgnoreCase("q")){
			_q_count++;
			String _table_field_names = "q_desc, q_level, sub_cat_id";			
			String _table_field_values = _tp_instance.get_q_desc()+", "+_tp_instance.get_level()+", "+_sub_cat_id;
			//new QueryMaker().createInsertQuery("question", _table_field_names, _table_field_values);
			
			_table_field_names = "q_id";
			//Map<String,String> _select_result = new QueryMaker().selectQuery("questions", _table_field_names, true);			
			//int _q_id = Integer.parseInt(_select_result.get("q_id")); // uncomment when connect with DB after implementation of QuerMaker class
			int _q_id = 1;
			
			// insert into answer table
			_table_field_names = "q_id, q_option_id";
			_table_field_values = _q_id+", "+_tp_instance.get_ans();
			//new QueryMaker().createInsertQuery("answers", _table_field_names, _table_field_values);
			
			
			//insert into options table.
			Iterator it = _option_list.entrySet().iterator();				
			while(it.hasNext()){
				Map.Entry pairs = (Map.Entry)it.next();
				
				_table_field_names = "q_id, q_option_id, q_option_desc";
				_table_field_values = _q_id+", "+ pairs.getKey() + ", "+pairs.getValue();
				//new QueryMaker().createInsertQuery("options", _table_field_names, _table_field_values);				
			}			
			_q_flag = 0;
		}
		
	}
		
}
